package com.erp.order.dn.dao.hibernate;

import com.erp.order.dn.dao.DeliveryNoteHeadDao;
import com.erp.order.dn.dao.model.DeliveryNoteHead;
import com.erp.order.dn.dao.model.DeliveryNoteHeadCO;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhoupan
 * @date 2021/4/9 11:22
 * @info
 */
@Repository
public class DeliveryNoteHeadDaoImpl implements DeliveryNoteHeadDao {
    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;

    @Override
    public void insertDataObject(DeliveryNoteHead deliveryNoteHead) {
        daoSupport.insertDataTransaction(deliveryNoteHead);
    }

    @Override
    public void updateDataObject(DeliveryNoteHead deliveryNoteHead) {

    }

    @Override
    public void insertOrUpdateDataObject(DeliveryNoteHead deliveryNoteHead) {

    }

    @Override
    public void deleteDataObject(DeliveryNoteHead deliveryNoteHead) {

    }

    @Override
    public List<DeliveryNoteHead> getDataObjects() {
        return null;
    }

    @Override
    public DeliveryNoteHead getDataObject(int i) {
        return null;
    }

    @Override
    public DeliveryNoteHead getDataObject(String s) {
        return null;
    }

    @Override
    public List<DeliveryNoteHead> getDataObjects(DeliveryNoteHeadCO deliveryNoteHeadCO) {
        return null;
    }

    @Override
    public List<DeliveryNoteHead> getDataObjects(Pages pages) {
        return null;
    }

    @Override
    public List<DeliveryNoteHead> getDataObjects(Pages pages, DeliveryNoteHeadCO paramObj) {
        String sql = "select m.* from dn_delivery_note_head m where 1=1 and m.is_del = '0'";
        if(StringUtils.isNotEmpty(paramObj.getDeliveryCode())){
            paramObj.setDeliveryCode("%"+ paramObj.getDeliveryCode() + "%");
        }
        if(StringUtils.isNotEmpty(paramObj.getDeliveryName())){
            paramObj.setDeliveryName("%"+ paramObj.getDeliveryName() + "%");
        }
        if(StringUtils.isNotEmpty(paramObj.getSupplier())){
            paramObj.setSupplier("%"+ paramObj.getSupplier() + "%");
        }
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLLikeCondition(paramObj, "deliveryCode", "and m.", args);
        sql = sql + DaoCRUDUtil.getSQLLikeCondition(paramObj, "deliveryName", "and m.", args);
        sql = sql + DaoCRUDUtil.getSQLLikeCondition(paramObj, "supplier", "and m.", args);
        sql = sql + " order by m.id desc";

        this.daoSupport.validateDataObject(paramObj);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("m", DeliveryNoteHead.class);

        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, DeliveryNoteHeadCO deliveryNoteHeadCO) {
        return null;
    }

    @Override
    public List<DeliveryNoteHead> getDataObjectsForDataAuth(String s, Pages pages, DeliveryNoteHeadCO deliveryNoteHeadCO) {
        return null;
    }

    @Override
    public DeliveryNoteHead getDataObjectByCode(String headCode) {
        String sql = "select d.* from dn_delivery_note_head d where d.is_del = '0' and d.delivery_code = :headCode";

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("headCode", headCode);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("d", DeliveryNoteHead.class);

        List<DeliveryNoteHead> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public Integer getNewDeliveryCode() {
        String sql =
                "SELECT " +
                        "nh.* " +
                        "FROM " +
                        "dn_delivery_note_head nh " +
                        "WHERE " +
                        "nh.is_del = '0' " +
                        "AND " +
                        "DATE_FORMAT(nh.created_date, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')";
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("nh", DeliveryNoteHead.class);
        List<DeliveryNoteHead> result = daoSupport.selectDataSql(sql, entity);
        return result == null ? 0 : result.size();
    }
}
